// 导入需要的config和request
import confing from '../config/config.js';
import request from '../util/request.js';
//申明请求对象
const templateRequest = {

    //defect请求的基本url
    BASE_URL: confing.QC_URL + "/template",


    /**
     * 保存自动生成templateCode的值
     * @param {*} templateCode
     * @param {*} callback 
     */
    generateTemplateCode(callback) {

        request.get(this.BASE_URL + "/generate", callback);

    },

    /**
     * 保存常见缺陷信息
     * @param {*} template 
     * @param {*} callback 
     */
    save(template, callback) {
        console.log(template + "======");
        if (template) {
            request.post(this.BASE_URL + '/save', template, callback);
        }
    },

    /**
        * 编辑缺陷信息
        * @param {*} template 
        * @param {*} callback 
        */
    update(template, callback) {
        if (template != undefined && template.templateCode != undefined) {
            request.post(this.BASE_URL + "/update", template, callback);
        }
    },
    /**
        * 根据indexCode加载常见缺陷信息
        * @param {*} templateCode 
        * @param {*} callback 
     */
    get(templateCode, callback) {
        if (templateCode) {
            let url = this.BASE_URL + "/get?templateCode=" + templateCode;
            request.get(url, callback);
        }
    },
    /**
      * 根据indexCode删除常见缺陷信息
      * @param {*} indexCode 
      * @param {*} callback 
   */
    delete(templateCode, callback) {
        if (templateCode) {
            let url = this.BASE_URL + "/delete?templateCode=" + templateCode;
            request.delete(url, callback);
        }
    },


    //请求的API
    list(page = 1, limit = 10, template, callback) {
        //url
        let url = this.BASE_URL + "/list?page=" + page + "&limit=" + limit;
        if (template) {
            if (template.templateCode) {
                url += "&templateCode=" + template.templateCode;
            }
            if (template.templateName) {
                url += "&templateName=" + template.templateName;
            }

            if (template.qcTypes) {
                url += "&qcTypes=" + template.qcTypes;
            }

        }
        //发送请求
        request.get(url, callback);

    }
}

//将defectRequest暴漏出去
export default templateRequest;
